﻿@using OA.AspNetCore.Html;
@using OA.Model;
@model OA.Model.SynergyForm
@{

    ViewBag.Title = Model.Name;

    CustomFormExtendInfo infoCustomFormExtendInfo = ViewBag.CustomFormExtendInfo;

    TableValues infoTableValues = ViewBag.TableValues;
    Dictionary<string, string> dicExtend = ViewBag.DicExtend;

    Layout = "~/Areas/Member/Views/Shared/_Layout.cshtml";
    string msg = ViewBag.Msg;
    string tips = string.Empty;
    if (!string.IsNullOrWhiteSpace(msg))
    {
        tips = $"layer.msg('{msg}');";
    }
}

<style type="text/css">
    .delete {
        cursor: pointer;
    }
</style>
<div class="layui-my-content">
    <div class="myForm">
        @using (Html.BeginForm(FormMethod.Post, htmlAttributes: new { @class = "layui-form", id = "myForm" }))
        {
            @Html.AntiForgeryToken()

            <div class="layui-form-item">
                @Html.LabelFor(model => model.Name, new { @class = "layui-form-label" })
                <div class="layui-input-block">
                    <input type="hidden" value="@Model.ID" name="ID" />
                    <input type="hidden" value="@Model.Status" name="Status" />
                    @Html.MyEditorFor(model => model.Name)
                </div>
            </div>
            if (infoCustomFormExtendInfo != null && infoCustomFormExtendInfo.ListExtendField.Count > 0)
            {
                foreach (var item in infoCustomFormExtendInfo.ListExtendField)
                {
                    if (dicExtend.ContainsKey(item.Attribute))
                    {
                        item.Value = dicExtend[item.Attribute];
                    }
                    <div class="layui-form-item">
                        <label class="layui-form-label" for="@item.Attribute">@item.Name</label>
                        <div class="layui-input-block">
                            @Html.Raw(MvcHtmlExtension.GetMyEditorControl(item, "dicExtend."))
                        </div>
                    </div>
                }

            }

            if (infoCustomFormExtendInfo != null && infoCustomFormExtendInfo.TableFields.Count > 0)
            {
                var parents = infoCustomFormExtendInfo.TableFields.Where(m => !string.IsNullOrWhiteSpace(m.ParentName)).ToList();
                var myGroups = parents.GroupBy(m => m.ParentName).ToList();
                <div class="layui-form-item">
                    <label class="layui-form-label">详情</label>
                    <div class="layui-input-block">
                        <table class="layui-table">
                            <thead>
                                @if (infoCustomFormExtendInfo.TableFields != null)
                                {
                                    if (myGroups.Count != parents.Count)
                                    {
                                        <tr>
                                            @foreach (var group in myGroups)
                                            {
                                                int children = group.Count();
                                                if (children == 1)
                                                {
                                                    <td rowspan="2" style="text-align:center;">@group.Key</td>
                                                }
                                                else
                                                {
                                                    <td colspan="@children" style="text-align:center;">@group.Key</td>
                                                }
                                            }
                                            <td rowspan="2" style="text-align:center;">#</td>
                                        </tr>
                                    }
                                }
                                <tr>
                                    @foreach (var item in infoCustomFormExtendInfo.TableFields)
                                    {
                                         <th>@item.Name</th>
                                        string jsContent = string.Empty;                                 

                                        if (item.Type == 69 || item.Type == 68)
                                        {
                                            if (!string.IsNullOrWhiteSpace(item.Values))
                                            {
                                                string[] myFields = item.Values.Split(new string[] { "+", "-", "*", "/", "(", ")", "[", "]" }, StringSplitOptions.RemoveEmptyEntries);
                                                List<string> fields = new List<string>();
                                                string objs = string.Empty;
                                                foreach (string key in myFields)
                                                {
                                                    double temp = 0;
                                                    if (!double.TryParse(key, out temp))
                                                    {
                                                        fields.Add("." + key);
                                                        objs += "var " + key + "=parseFloat(trObj.find('." + key + "').val());\r\n";
                                                    }
                                                }
                                                if (fields.Count > 0)
                                                {
                                                    string objTotal = string.Join(",", fields);
                                                    objs += "trObj.find('." + item.Attribute + "').val(Math.formatFloat(" + item.Values + ",2));\r\n";
                                                    jsContent += "<script type='text/javascript'>$(document).on('change', '" + objTotal + "', function(){\r\nvar trObj = $(this).parent().parent();\r\n" + objs + "});</script>";

                                                }
                                            }
                                        }
                                        @Html.Raw(jsContent);
                                    }
                                    @if (myGroups.Count == infoCustomFormExtendInfo.TableFields.Count)
                                    {
                                        <td style="text-align:center;">#</td>
                                    }
                                </tr>
                            </thead>
                            <tbody>
                                @*@for (int i = 0; i < infoTableValues.RowValues.Count; i++)
                                {
                                    RowValue rowValue = infoTableValues.RowValues[i];
                                    if (rowValue.CellValues == null)
                                    {
                                        rowValue.CellValues = new Dictionary<string, string>();
                                    }
                                    <tr data-id="@i">
                                        @for (int j = 0; j < infoCustomFormExtendInfo.TableFields.Count; j++)
                                        {
                                            ExtendField infoExtendField = new ExtendField();
                                            infoExtendField = infoCustomFormExtendInfo.TableFields[j];
                                            var cellValue = string.Empty;
                                            if (rowValue.CellValues.ContainsKey(infoExtendField.Attribute))
                                            {
                                                infoExtendField.Value = rowValue.CellValues[infoExtendField.Attribute];
                                            }
                                            else
                                            {
                                                infoExtendField.Value = infoExtendField.DefaultValue;
                                            }
                                            <td>
                                                @Html.Raw(MvcHtmlExtension.GetMyEditorControl(infoExtendField, "tableValues.RowValues[" + i.ToString() + "].CellValues."))
                                            </td>
                                        }
                                        <td>
                                            <input type="hidden" class="Status" value="1" name="tableValues.RowValues[@i].Status" />
                                            <span class="layui-btn layui-btn-xs layui-btn-danger deleteItem">删除</span>
                                        </td>
                                    </tr>
                                }*@
                            </tbody>
                            <tfoot>
                                @{
                                    int colspan = infoCustomFormExtendInfo.TableFields.Count + 1;
                                }
                                <tr><td colspan="@colspan" class="right_content"><button type="button" class="layui-btn layui-btn-normal addItem">增加一行</button></td></tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            }

            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align:right;">
                    <button type="submit" class="layui-btn mySubmit" lay-submit="" lay-filter="myForm"> 提  交 </button>
                </div>
            </div>
        }
    </div>
</div>

<script type="text/javascript" src="/Member/Home/GetDepartmentAndPosition"></script>
<script type="text/javascript" src="@ViewBag.OssUrl/front/js/department.js"></script>
<script type="text/javascript">
    var myFrom = $("#myForm");
    function mySubmit() {
        $.ajax({
            url: myFrom.prop("action"),
            type: myFrom.prop("method"),
            data: myFrom.serialize(),
            dataType: "json",
            beforeSend: function () {
                layer.msg("正在提交，请稍后...");
            },
            error: function () {
                $(".mySubmit").prop("disabled", false);
            },
            complete: function () {

            },
            success: function (data) {
                if (data.State == 400) {
                    layer.msg(data.Msg);
                    $(".mySubmit").prop("disabled", false);
                    return false;
                }
                if (data.State == 402) {
                    layer.msg(data.Msg);
                    $(".mySubmit").prop("disabled", false);
                    return false;
                }
                else {
                    layer.msg(data.Msg);
                    window.location.href = "/Member/Synergy/Show?id=@Model.ID";
                    return false;
                }
            }
        });
    }

    layui.use(['form', 'layedit', 'autocomplete', 'element', 'layer','laydate'], function () {
		form = layui.form;
        layer = layui.layer;
        element = layui.element;
        laydate = layui.laydate;
        form.on('submit(myForm)', function (data) {
            $(".mySubmit").prop("disabled", true);
            mySubmit();
            return false;
        });
        @Html.Raw(tips)


    });

    $(".addItem").click(function () {
        var my_tbody = $(this).parent().parent().parent().parent().find("tbody");
        var last = my_tbody.find("tr:last");
        var lastID = last.attr("data-id");
        var currentIndex = parseInt(lastID) + 1;
        var outerHTML = last.prop('outerHTML');
        var reg = new RegExp("\\[" + lastID + "\\]", "g");
        var reg2 = new RegExp("_" + lastID + "_", "g");
        var reg3 = new RegExp("lay-key=\"\\d+\"", "g");
        outerHTML = outerHTML.replace('data-id="' + lastID + '"', 'data-id="' + currentIndex + '"');
        outerHTML = outerHTML.replace(reg, "[" + currentIndex + "]");
        outerHTML = outerHTML.replace(reg2, "_" + currentIndex + "_");
        outerHTML = outerHTML.replace(reg3, "");
        my_tbody.append(outerHTML);
        my_tbody.find("tr:last").find("input").val("");
        my_tbody.find("tr:last").css("display", "");
        layui.use('form', function () {
            var form = layui.form;
            form.render();
        });
    });

    $(document).on("click", ".delete", function () {
        $(this).parent().parent().find("input").addClass(".isHidden").removeClass("mydate");
        $(this).parent().parent().find("input").val("0");
        $(this).parent().parent().find(".Status").val("-1");
        $(this).parent().parent().find("input").removeAttr("lay-verify");
        $(this).parent().parent().css("display", "none");
    });


    $(document).on("click", ".deleteItem", function () {
        $(this).parent().parent().find("input").addClass(".isHidden").removeClass("mydate");
        $(this).parent().parent().find("input").val("0");
        $(this).parent().parent().find(".Status").val("-1");
        $(this).parent().parent().find("input").removeAttr("lay-verify");
        $(this).parent().parent().css("display", "none");
    });


</script>
